package edu.washington.cs.workflow.recommender;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import edu.washington.cs.workflow.analysis.guistructure.UIActionHandler;
import junit.framework.TestCase;

public class TestParsingTraceFileCrossword extends TestCase {
	
	//the trace file produced by the new version
	public void testNewCrossword() {
		String file = "./tests/files/crossword_clicknew_new_version.txt";
		EventTraceProcessor processor = new EventTraceProcessor(file);
		List<UIActionHandler> handlers = processor.getAllActionHandlers();
		assertEquals(2, handlers.size());
		
		Set<String> handlerDescs = new HashSet<String>();
		for(UIActionHandler handler : handlers) {
			handlerDescs.add(handler.methodDesc);
		}
		
		for(String handlerDesc : handlerDescs) {
			System.out.println("Handler: " + handlerDesc);
			List<String> ms = processor.getInvokedMethodsByHandler(handlerDesc);
			System.out.println("   " + ms);
		}
	}

	//the trace file produced by the old version
	//go to the experiments, and use the following command:
	//  java -cp ./crossword-0.3-instr.jar;../bin;../libs/simmetrics_jar.jar crosswordsage.MainScreen
	public void testNewCrossword_old() {
		String newFile = "./tests/files/crossword_all_actions_new_version.txt";
		EventTraceProcessor traceProcessor = new EventTraceProcessor(newFile);
		
		String file = "./tests/demo-traces/crossword-new-puzzle-old-version.txt";
		EventTraceProcessor processor = new EventTraceProcessor(file);
//		List<UIActionHandler> handlers = processor.getAllActionHandlers();
		List<String> handlerDescs = processor.getAllUniqueActionHandlerDescs();
		for(String desc : handlerDescs) {
			System.out.println(desc);
			System.out.println("      actions: " + traceProcessor.getTriggeringActionDescs(desc));
			System.out.println("its invoked method: ");
			List<String> methods = processor.getInvokedMethodsByHandler(desc);
			for(String m : methods) {
				System.out.println("   " + m);
				//then query the 
				List<String> descs = traceProcessor.getTriggeringActionDescs(m);
				System.out.println("      actions: " + descs);
				
				if(m.equals("crosswordsage.MainScreen.showCrosswordBuilder()V")) {
					assertEquals(1, descs.size());
				}
			}
		}
	}
	
	public void testGetAllActions() {
		String file = "./tests/demo-traces/crossword-new-puzzle-old-version.txt";
		EventTraceProcessor processor = new EventTraceProcessor(file);
		List<String> handlerDescs = processor.getAllUniqueActionHandlerDescs();
		System.out.println(handlerDescs);
		for(String actionDesc : handlerDescs) {
			System.out.println(actionDesc);
		}
	}
}
